package org.kineticsproject.visualizations;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.NumberCellRenderer;






public class MultiplePlotChart
{
	


	public JFreeChart setupChart(List datasetList,String chartTitle, String tabTitle, org.jfree.chart.renderer.xy.XYItemRenderer renderer) 
	{
			JFreeChart 				xyChart;
			XYDataset 				curDataset;
			XYSeriesExt				curXYSeriesExt;
			Iterator   				it 				= datasetList.iterator();
			CombinedDomainXYPlot 	plot 			= null;
			XYPlot              	subplot         = null;
			ChartPanelExt 			xyChartPanel 	= null;
			JTable                  statsTable      = null;
			//StatsTableModel         statsTableModel = null;
			int                     row             = 0;
			JPanelExt               contentPanel   	= null;
			//JPanel                  tablePanel     	= null;
			
			curXYSeriesExt = (XYSeriesExt)datasetList.get(0);
			if (curXYSeriesExt.getXLabel().compareTo("Time") == 0)
				plot 									= new CombinedDomainXYPlot(new DateAxis("Time"));
			else
			plot									= new CombinedDomainXYPlot(new NumberAxis(curXYSeriesExt.getXLabel()+" ("+curXYSeriesExt.getXUnits()+")"));
			//statsTableModel 						= new StatsTableModel(datasetList.size());
			contentPanel                           	= new JPanelExt(new GridBagLayout());
			//tablePanel                              = new JPanel(new BorderLayout());
			//tablePanel.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));		
            
			while(it.hasNext())
			{
				curXYSeriesExt 	= (XYSeriesExt)it.next();
				curDataset 		= new XYSeriesCollection(curXYSeriesExt);
			
				/* Setup Statistics Table */
		        //statsTableModel.setValueAt(curXYSeriesExt.getYLabel(), row, 0); 			// Column 0 -> Series Name
		       // statsTableModel.setValueAt(curXYSeriesExt.getMean(), row, 1);   			// Column 1 -> Mean
		       // statsTableModel.setValueAt(curXYSeriesExt.getStandardDeviation(), row, 2);	// Column 2 -> Standard Deviation
		        ++row;
		        
		        /* Setup Subplot */
		        NumberAxis axis = new NumberAxis(curXYSeriesExt.getYLabel()+" ("+curXYSeriesExt.getYUnits()+")");       
		        axis.setAutoRangeIncludesZero(true);
		        //axis.setRange(curXYSeriesExt.getRangeMin(),curXYSeriesExt.getRangeMax());
		        subplot = new XYPlot( curDataset, null, axis, /*new StandardXYItemRenderer()*/ renderer);
		        subplot.setBackgroundPaint(Color.white);
		        subplot.setDomainGridlinePaint(Color.black);
		        subplot.setRangeGridlinePaint(Color.black);	  
		        
		        
		        plot.add(subplot);

			}
			
			
			//TableCellRenderer numberRenderer 	= new NumberCellRenderer();          

			
			//statsTable 							= new JTable(statsTableModel);
			//statsTable.getColumnModel().getColumn(1).setCellRenderer(numberRenderer);
			//statsTable.getColumnModel().getColumn(2).setCellRenderer(numberRenderer);
			
			
			xyChart = new JFreeChart(chartTitle, plot);
			xyChartPanel = new ChartPanelExt(xyChart, chartTitle, tabTitle);

			xyChartPanel.setDomainZoomable(true);
			xyChartPanel.setRangeZoomable(true);
            xyChartPanel.setBorder(BorderFactory.createCompoundBorder(
            		BorderFactory.createEtchedBorder(),
                    BorderFactory.createEmptyBorder(10, 10, 10, 10)
            ));
           // xyChartPanel.setVerticalAxisTrace(true);
           // xyChartPanel.setHorizontalAxisTrace(true);
            
            GridBagConstraints c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 0;
            c.weightx = 1.0;
            c.weighty = 1.0;
            c.fill = GridBagConstraints.BOTH;
            c.anchor = GridBagConstraints.FIRST_LINE_START;
            c.insets = new Insets(12, 12, 0, 12);
            contentPanel.add(xyChartPanel, c);
       
            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.LAST_LINE_START;
            c.insets = new Insets(12, 12, 12, 12);
           // statsTable.setPreferredScrollableViewportSize(statsTable.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane(statsTable);
            contentPanel.add(scrollPane, c);
			contentPanel.setTabTitle(xyChartPanel.getChartTitle());
			
			return(xyChart);
	}
	

	

}
